<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/7/21
 * Time: 11:13
 */

namespace pay\modules\frontend\models;


class Order extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_order';
    }


    public function rules()
    {
        return [
            ['app_id','required','message' => 'appId不能为空'],
            ['aid','required','message' => '应用id，不能为空'],
            ['pay_order_sn','required','message' => '支付单号不能为空'],
            ['mar_order_sn','required','message' => '商户单号不能为空'],
            [['pay_order_sn','mar_order_sn','ref_order_sn'],'string', 'max' => 128],
            ['real_money','integer'],
            ['wechat_order_sn','string', 'max' => 64],
            [['pay_times', 'create_time'], 'safe'],
        ];
    }


    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'oid' => 'Oid',
            'app_id' => 'App ID',
            'aid' => '应用id',
            'pay_order_sn' => '支付单号',
            'mar_order_sn' => '商户单号',
            'wechat_order_sn' => '微信单号',
            'ref_order_sn' => '退款单号',
            'real_money' => '支付金额',
            'pay_status' => '支付状态',
            'pay_times' => '支付时间',
            'create_time' => '创建时间',
        ];
    }

    /*** 添加订单信息
     * @param $data
     * @return bool|mixed
     */
    public function addOrder($data)
    {
        $data['Order']['app_id'] = $data['appId'];
        $data['Order']['aid'] = $data['aid'];
        $data['Order']['pay_order_sn'] = $data['pay_order_sn'];
        $data['Order']['mar_order_sn'] = $data['payNo'];
        $data['Order']['pay_status'] = '1';
        $data['Order']['wechat_order_sn'] = '';
        $data['Order']['real_money'] = 0;
        $data['Order']['create_time'] = date("Y-m-d H:i:s");
        if ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return $this->primaryKey;
            }
        }
        return false;
    }


    /*** 查询订单信息
     * @param $appid    实例id
     * @param $payNo    商户订单号
     * @return array|null|\yii\db\ActiveRecord
     */
    public function queryOrderInfo($appid,$payNo)
    {
        $data = self::find()->where('app_id = :aid and mar_order_sn = :msn',[':aid' => $appid,':msn' => $payNo])->asArray()->one();
        if (!empty($data)){
            unset($data['aid'],$data['app_id']);
        }
        return $data;
    }
	
	public function queryPayOrderInfoByRemake($mar_order_sn)
	{
		$detail = OrderDetails::find()->where(['like','remake',$mar_order_sn])->asArray()->one();
		$data=[];
		if(!empty($detail)){
			$data = self::find()->where(['oid'=>$detail['oid']])->asArray()->one();
		}
		if (!empty($data)){
		    unset($data['aid'],$data['app_id']);
		}
		return $data;
	}


    public function queryPayOrderInfo($mar_order_sn)
    {
        $data = self::find()->where('mar_order_sn = :psn',[':psn' => $mar_order_sn])->asArray()->one();
        if (!empty($data)){
            unset($data['aid'],$data['app_id']);
        }
        return $data;
    }


    public function getInfo($payNo,$appId)
    {
        $model = self::find()
            ->where('mar_order_sn = :msn and app_id = :aid',[':msn' => $payNo,':aid' => $appId])
            ->asArray()->one();

        return $model;
    }


}